package com.aizhixin.lab.project.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.template.domain.ExercisesDomain;
import com.aizhixin.lab.project.template.service.ExercisesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/project/exercises")
@RestController
@Api(tags = "习题", value = "习题")
public class ExercisesController {
    @Autowired
    private ExercisesService exercisesService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增习题", httpMethod = "POST", notes = "新增习题<br><br><b>lwq</b>")
    public ResponseEntity<?> findTaskInfo(@RequestHeader("Authorization") String token, @RequestBody List<ExercisesDomain> domainList) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        result = exercisesService.saveQuestionList(domainList, account);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findAllQuestions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看任务或工单下所有试题", httpMethod = "GET", notes = "查看任务或工单下所有试题<br><br><b>lwq</b>")
    public ResponseEntity<?> findTaskInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<ExercisesDomain> exercisesDomainList = exercisesService.findAllQuestions(id);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.DATA, exercisesDomainList);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/importExercises", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导入习题", httpMethod = "POST", notes = "导入习题<br><br><b>@author lwq</b>")
    public ResponseEntity<?> importFile(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "Excel数据文件", required = true) @RequestParam(value = "file") MultipartFile file) throws IOException {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            exercisesService.exportExercises(file, account.getId());
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.MESSAGE, "导入成功");
        } catch (NullPointerException e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.MESSAGE, e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除任务或工单下的习题", httpMethod = "DELETE", notes = "删除任务或工单下的习题<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        exercisesService.delete(id);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "删除成功");
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
